package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hd.appcommon.entity.SysParam;
import com.hd.appcommon.mapper.SysParamMapper;
import com.hd.appcommon.service.ISysParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.hd.appcommon.constant.SysParamConstant.CONNECTOR;
import static com.hd.appcommon.constant.SysParamConstant.ROOT_ID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2020-12-07
 */
@Service
public class SysParamServiceImpl extends ServiceImpl<SysParamMapper, SysParam> implements ISysParamService {


    @Override
    public List<SysParam> listByParentId(String parentId) {
        LambdaQueryWrapper<SysParam> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(SysParam::getParamId, parentId).orderByDesc(SysParam::getParamRank);
        return this.list(wrapper);
    }

    @Override
    public List<SysParam> listByParentType(String parentType) {
        LambdaQueryWrapper<SysParam> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(SysParam::getParamType, parentType).orderByDesc(SysParam::getParamRank);
        return this.list(wrapper);
    }

    @Override
    public List<Map<String, Object>> listMapByParentType(String parentType) {
        List<SysParam> list = this.listByParentType(parentType);
        List<Map<String, Object>> mapList = list.stream().map(field -> {
            Map<String, Object> map = getMap(field);
            map.put("checked", false);
            return map;
        }).collect(Collectors.toList());
        Map<String, Map<String, Object>> treeMap = mapList.stream()
                .collect(Collectors.toMap(p -> (String) (p.get("id")), p -> p));
        List<Map<String, Object>> trees = new LinkedList<>();
        mapList.forEach(map -> {
            Map<String, Object> parent = treeMap.get(map.get("pid"));
            if (parent != null) {
                if (parent.get("children") == null)
                    parent.put("children", new LinkedList<Map<String, Object>>());
                ((List<Map<String, Object>>) (parent.get("children"))).add(map);
            } else
                trees.add(map);
        });
        return trees;
    }

    @Override
    public String getFullNameByParamId(String paramId, List<SysParam> paramList) {
        int circle = (paramId.length() + 1) / 5;
        List<String> paramIds = new ArrayList<>();
        for (int i = 1; i <= circle; i++) {
            paramIds.add(paramId.substring(0, (5 * i) - 1));
        }
        List<String> paramNames = new ArrayList<>();
        for (int i = 0; i < paramList.size(); i++) {
            SysParam sysParam = paramList.get(i);
            for (int j = 0; j < paramIds.size(); j++) {
                String s = paramIds.get(j);
                if (sysParam.getParamId().equals(s)) {
                    paramNames.add(sysParam.getParamName());
                }
            }
        }
        return paramNames.stream().collect(Collectors.joining("-"));
    }

    private Map<String, Object> getMap(SysParam sysParam) {
        Map<String, Object> map = new HashMap<>();
        String paramId = sysParam.getParamId();
        map.put("id", paramId);
        map.put("name", sysParam.getParamName());
        int lastIndexOf = paramId.lastIndexOf(CONNECTOR);
        if (lastIndexOf > 0) {
            map.put("pid", paramId.substring(0, lastIndexOf));
        } else {
            map.put("pid", ROOT_ID);
        }
        return map;
    }
}
